﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;

namespace web.Common
{
    public class ReflectionForm
    {
        public static T GetData<T>(T model) where T : new()
        {

            try
            {
                var properties = new T().GetType().GetProperties();
                var r = properties;
                foreach (var p in properties)
                {
                    string pval = System.Web.HttpContext.Current.Request[p.Name + ""];
                    if (System.Web.HttpContext.Current.Request[p.Name + ""] != null)
                    {
                        if (pval == "" || pval == "null")
                        {
                            p.SetValue(model, null, null);
                            continue;
                        }
                        // pval = pval.Split(',')[0];
                        string pptypeName = p.PropertyType.Name;
                        if (pptypeName.LastIndexOf("Int") != -1)
                        {
                            p.SetValue(model, Convert.ToInt32(pval), null);
                        }
                        else if (pptypeName == "DateTime")
                        {
                            p.SetValue(model, Convert.ToDateTime(pval), null);
                        }
                        else if (pptypeName == "String")
                        {
                            p.SetValue(model, pval, null);
                        }
                        else if (pptypeName == "Decimal")
                        {
                            p.SetValue(model, Convert.ToDecimal(pval), null);
                        }
                        else if (pptypeName == "Guid")
                        {
                            p.SetValue(model, Guid.Parse(pval), null);
                        }
                        else if (pptypeName == "Boolean")
                        {
                            p.SetValue(model, Convert.ToBoolean(pval), null);
                        }

                        else if (pptypeName == "Nullable`1")
                        {
                            pptypeName = p.PropertyType.FullName;
                            if (pptypeName.LastIndexOf("Int") != -1)
                            {
                                p.SetValue(model, Convert.ToInt32(pval), null);
                            }
                            else if (pptypeName.LastIndexOf("DateTime") != -1)
                            {
                                p.SetValue(model, Convert.ToDateTime(pval), null);
                            }
                            else if (pptypeName.LastIndexOf("String") != -1)
                            {
                                p.SetValue(model, pval, null);
                            }
                            else if (pptypeName.LastIndexOf("Decimal") != -1)
                            {
                                p.SetValue(model, Convert.ToDecimal(pval), null);
                            }
                            else if (pptypeName.LastIndexOf("Guid") != -1)
                            {
                                p.SetValue(model, Guid.Parse(pval), null);
                            }
                            else if (pptypeName.LastIndexOf("Boolean") != -1)
                            {
                                p.SetValue(model, Convert.ToBoolean(pval), null);
                            }
                        }
                    }
                }
                return model;


            }
            catch (Exception ex)
            {


                throw ex;
            }



        }

        public static T GetData<T>(T model, DataRow dr, DataColumnCollection dataColumnCollection) where T : new()
        {

            try
            {
                var properties = new T().GetType().GetProperties();
                var r = properties;
                foreach (var p in properties)
                {

                    if (dataColumnCollection.Contains(p.Name))
                    {
                        if (dr[p.Name] != null && dr[p.Name].ToString() != "")
                        {
                            string pval = dr[p.Name].ToString();
                            if (pval == "")
                            {
                                p.SetValue(model, null, null);
                                continue;
                            }
                            // pval = pval.Split(',')[0];
                            string pptypeName = p.PropertyType.Name;
                            if (pptypeName.LastIndexOf("Int") != -1)
                            {
                                p.SetValue(model, Convert.ToInt32(pval), null);
                            }
                            else if (pptypeName == "DateTime")
                            {
                                p.SetValue(model, Convert.ToDateTime(pval), null);
                            }
                            else if (pptypeName == "String")
                            {
                                p.SetValue(model, pval, null);
                            }
                            else if (pptypeName == "Decimal")
                            {
                                p.SetValue(model, Convert.ToDecimal(pval), null);
                            }
                            else if (pptypeName == "Guid")
                            {
                                p.SetValue(model, Guid.Parse(pval), null);
                            }
                            else if (pptypeName == "Boolean")
                            {
                                p.SetValue(model, Convert.ToBoolean(pval), null);
                            }

                            else if (pptypeName == "Nullable`1")
                            {
                                pptypeName = p.PropertyType.FullName;
                                if (pptypeName.LastIndexOf("Int") != -1)
                                {
                                    p.SetValue(model, Convert.ToInt32(pval), null);
                                }
                                else if (pptypeName.LastIndexOf("DateTime") != -1)
                                {
                                    p.SetValue(model, Convert.ToDateTime(pval), null);
                                }
                                else if (pptypeName.LastIndexOf("String") != -1)
                                {
                                    p.SetValue(model, pval, null);
                                }
                                else if (pptypeName.LastIndexOf("Decimal") != -1)
                                {
                                    p.SetValue(model, Convert.ToDecimal(pval), null);
                                }
                                else if (pptypeName.LastIndexOf("Guid") != -1)
                                {
                                    p.SetValue(model, Guid.Parse(pval), null);
                                }
                                else if (pptypeName.LastIndexOf("Boolean") != -1)
                                {
                                    p.SetValue(model, Convert.ToBoolean(pval), null);
                                }
                            }
                        }
                    }
                }
                return model;


            }
            catch (Exception ex)
            {


                throw ex;
            }



        }




    }
}